

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cai Shibo">
  <meta name="keywords" content="cc">
  
    <meta name="description" content="AOP常用动态代理技术  JDK 代理：基于接口的动态代理技术（一定要有接口）   底层代码示例：   123456789101112131415161718192021222324252627282930public static void main(String[] args) &amp;#123;    &#x2F;&#x2F;目标对象    final Target target &#x3D; new Target();">
<meta property="og:type" content="article">
<meta property="og:title" content="SSM--D9">
<meta property="og:url" content="https://hahsx_xd.gitee.io/2022/04/30/SSM-D9/index.html">
<meta property="og:site_name" content="🍊CAI SHIBO🥬">
<meta property="og:description" content="AOP常用动态代理技术  JDK 代理：基于接口的动态代理技术（一定要有接口）   底层代码示例：   123456789101112131415161718192021222324252627282930public static void main(String[] args) &amp;#123;    &#x2F;&#x2F;目标对象    final Target target &#x3D; new Target();">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://hahsx_xd.gitee.io/images/pasted-109.png">
<meta property="og:image" content="https://hahsx_xd.gitee.io/images/pasted-110.png">
<meta property="og:image" content="https://hahsx_xd.gitee.io/images/pasted-113.png">
<meta property="og:image" content="https://hahsx_xd.gitee.io/images/pasted-111.png">
<meta property="og:image" content="https://hahsx_xd.gitee.io/images/pasted-115.png">
<meta property="article:published_time" content="2022-04-30T08:12:54.000Z">
<meta property="article:modified_time" content="2022-11-29T07:14:46.501Z">
<meta property="article:author" content="Cai Shibo">
<meta property="article:tag" content="cc">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://hahsx_xd.gitee.io/images/pasted-109.png">
  
  
  <title>SSM--D9 - 🍊CAI SHIBO🥬</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"hahsx_xd.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":".|","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":"#"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":5},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":true,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="🍊CAI SHIBO🥬" type="application/atom+xml">
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>^_^ 🥬 CC</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="SSM--D9">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      Cai Shibo
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-04-30 16:12" pubdate>
        2022年4月30日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      5.1k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      26 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">SSM--D9</h1>
            
              <p class="note note-info">
                
                  最后更新：23 天前
                
              </p>
            
            <div class="markdown-body">
              <h3 id="AOP"><a href="#AOP" class="headerlink" title="AOP"></a>AOP</h3><p>常用动态代理技术</p>
<ul>
<li><p>JDK 代理：基于接口的动态代理技术（一定要有接口）  </p>
<p><strong>底层代码示例：</strong></p>
  <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>    <span class="hljs-comment">//目标对象</span><br>    <span class="hljs-keyword">final</span> Target target = <span class="hljs-keyword">new</span> Target();<br><br>    <span class="hljs-comment">//增强对象</span><br>    Advice advice = <span class="hljs-keyword">new</span> Advice();<br><br>    <span class="hljs-comment">//返回值：动态生成的代理对象</span><br>    TargetInterface targetProxy =  (TargetInterface) Proxy.newProxyInstance(<br>            target.getClass().getClassLoader(), <span class="hljs-comment">//目标对象类加载器</span><br>            target.getClass().getInterfaces(),  <span class="hljs-comment">//目标对象相同的接口字节码对象数组</span><br>            <span class="hljs-keyword">new</span> InvocationHandler() &#123;<br>                <span class="hljs-comment">//调用代理对象的任何方法，实质执行的都是invoke方法</span><br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">invoke</span><span class="hljs-params">(Object proxy, Method method, Object[] args)</span> <span class="hljs-keyword">throws</span> Throwable </span>&#123;<br>                    <span class="hljs-comment">//前置增强</span><br>                    advice.before();<br>                    <span class="hljs-comment">//执行目标方法</span><br>                    Object invoke = method.invoke(target, args);<br>                    <span class="hljs-comment">//后指增强</span><br>                    advice.after();<br>                    <span class="hljs-keyword">return</span> invoke;<br>                &#125;<br>            &#125;<br>    );<br><br>    <span class="hljs-comment">//调用代理对象的方法</span><br>    targetProxy.save();<br><br>&#125;<br></code></pre></div></td></tr></table></figure></li>
<li><p>cglib 代理：基于父类的动态代理技术</p>
<p><strong>底层代码示例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProxyTest</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        <span class="hljs-comment">//目标对象</span><br>        <span class="hljs-keyword">final</span> Target target = <span class="hljs-keyword">new</span> Target();<br><br>        <span class="hljs-comment">//增强对象</span><br>        Advice advice = <span class="hljs-keyword">new</span> Advice();<br><br>        <span class="hljs-comment">//返回值：动态生成的代理对象 基于cglib</span><br>        <span class="hljs-comment">//1、创建增强器</span><br>        Enhancer enhancer = <span class="hljs-keyword">new</span> Enhancer();<br>        <span class="hljs-comment">//2、设置父类（目标）</span><br>        enhancer.setSuperclass(Target.class);<br>        <span class="hljs-comment">//3、设置回调</span><br>        enhancer.setCallback(<span class="hljs-keyword">new</span> MethodInterceptor() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">intercept</span><span class="hljs-params">(Object o, Method method, Object[] objects, MethodProxy methodProxy)</span> <span class="hljs-keyword">throws</span> Throwable </span>&#123;<br>                advice.before(); <span class="hljs-comment">//执行前置</span><br>                Object invoke = method.invoke(target, args); <span class="hljs-comment">//执行目标</span><br>                advice.after(); <span class="hljs-comment">//执行后置</span><br>                <span class="hljs-keyword">return</span> invoke;<br>            &#125;<br>        &#125;);<br>        <span class="hljs-comment">//4、创建代理对象</span><br>        Target targetProxy = (Target) enhancer.create();<br><br>        <span class="hljs-comment">//调用代理对象的方法</span><br>        targetProxy.save();<br><br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure></li>
</ul>
<h4 id="AOP-相关概念："><a href="#AOP-相关概念：" class="headerlink" title="AOP 相关概念："></a>AOP 相关概念：</h4><p><img src="/images/pasted-109.png" srcset="/img/loading.gif" lazyload></p>
<p>通俗地讲：</p>
<ul>
<li>连接点：可以被增强的方法（Spring 只支持方法类型的连接点）；</li>
<li>切入点：真正要增强的连接点；</li>
<li>通知/增强：对切入点方法进行增强的方法；</li>
<li>切面：切入点和通知的结合；</li>
<li>织入：形成切面的过程。</li>
</ul>
<h5 id="AOP-开发明确事项"><a href="#AOP-开发明确事项" class="headerlink" title="AOP 开发明确事项"></a>AOP 开发明确事项</h5><ol>
<li><p>需要编写的内容</p>
<ul>
<li>编写核心业务代码（目标类的目标方法）</li>
<li>编写切面类（Spring 已经封装），重点是编写切面类中的通知，即增强功能的方法</li>
<li>在配置文件中，配置织入关系，即哪些通知与切入点结合</li>
</ul>
</li>
<li><p>AOP 技术实现的内容<br>Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行，使用代理机制，动态创建目标对象的代理对象，根据通知类别，在代理对象的对应位置，将通知对应的功能织入，完成完整的代码逻辑运行。</p>
</li>
<li><p>AOP 底层：Spring 框架会根据目标类是否实现接口来决定采用哪种动态代理的方式。</p>
</li>
</ol>
<hr>
<h4 id="基于-XML-实现-AOP"><a href="#基于-XML-实现-AOP" class="headerlink" title="基于 XML 实现 AOP"></a>基于 XML 实现 AOP</h4><ol>
<li>导入 AOP 相关坐标  </li>
</ol>
<p>  <strong>pom.xml（相关坐标）</strong><br>  <figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.aspectj<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>aspectjweaver<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.8.4<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></div></td></tr></table></figure></p>
<ol start="2">
<li><p>创建目标接口和目标类（内部有切点）  </p>
<p><em>目标类</em></p>
 <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Target</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">TargetInterface</span> </span>&#123;<br>   <span class="hljs-comment">//切点</span><br>   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span><span class="hljs-params">()</span> </span>&#123;<br>       System.out.println(<span class="hljs-string">&quot;save running...&quot;</span>);<br>   &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure></li>
<li><p>创建切面类（内部有增强方法）  </p>
<p> <em>切面类</em></p>
 <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAspect</span> </span>&#123;<br>    <span class="hljs-comment">//增强方法</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">before</span><span class="hljs-params">()</span> </span>&#123;<br>       System.out.println(<span class="hljs-string">&quot;before enhance&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure></li>
<li><p>将目标类和切面类的对象创建权交给 spring（见步骤5下代码）</p>
</li>
<li><p>在 applicationContext.xml 中配置织入关系</p>
<p> <strong>applicationContext.xml（相关配置）</strong></p>
 <figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--目标对象--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;target&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.aop.Target&quot;</span>/&gt;</span><br><br><span class="hljs-comment">&lt;!--切面对象--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;myAspect&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.aop.MyAspect&quot;</span>/&gt;</span><br><br><span class="hljs-comment">&lt;!--配置织入，哪些方法（切点）需要进行哪些增强（前置、后置方法）--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">aop:config</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--声明切面--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">aop:aspect</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;myAspect&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--配置切面 = 切点 + 通知--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">aop:before</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;before&quot;</span> <span class="hljs-attr">pointcut</span>=<span class="hljs-string">&quot;execution(public void com.aop.Target.save())&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">aop:aspect</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">aop:config</span>&gt;</span><br></code></pre></div></td></tr></table></figure></li>
</ol>
<h5 id="详解"><a href="#详解" class="headerlink" title="详解"></a>详解</h5><h6 id="织入配置详解"><a href="#织入配置详解" class="headerlink" title="织入配置详解"></a>织入配置详解</h6><p><img src="/images/pasted-110.png" srcset="/img/loading.gif" lazyload></p>
<p><em>环绕通知的增强方法中一定要参数</em>  </p>
<p><strong>切面类部分代码</strong></p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-comment">//ProceedingJoinPoint 正在执行的连接点 = 切点</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">around</span><span class="hljs-params">(ProceedingJoinPoint proceedingJoinPoint)</span> <span class="hljs-keyword">throws</span> Throwable </span>&#123;<br>    System.out.println(<span class="hljs-string">&quot;before enhance&quot;</span>);<br>    Object proceed = proceedingJoinPoint.proceed();<span class="hljs-comment">//执行切点方法</span><br>    System.out.println(<span class="hljs-string">&quot;after enhance&quot;</span>);<br>    <span class="hljs-keyword">return</span> proceed;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<h6 id="增强方法类型"><a href="#增强方法类型" class="headerlink" title="增强方法类型"></a>增强方法类型</h6><p><img src="/images/pasted-113.png" srcset="/img/loading.gif" lazyload></p>
<h6 id="切点表达式"><a href="#切点表达式" class="headerlink" title="切点表达式"></a>切点表达式</h6><p><img src="/images/pasted-111.png" srcset="/img/loading.gif" lazyload></p>
<h6 id="切点表达式的抽取"><a href="#切点表达式的抽取" class="headerlink" title="切点表达式的抽取"></a>切点表达式的抽取</h6><p>当多个增强的切点表达式相同时，可以将切点表达式进行抽取，在增强中使用 pointcut-ref 属性来引用抽取后的切点表达式。</p>
<p><strong>applicationContext.xml（相关配置）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--切点表达式的抽取--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">aop:pointcut</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;myPointcut&quot;</span> <span class="hljs-attr">expression</span>=<span class="hljs-string">&quot;execution(void com..*())&quot;</span>/&gt;</span><br><span class="hljs-comment">&lt;!--使用抽取的表达式--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">aop:around</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;around&quot;</span> <span class="hljs-attr">pointcut-ref</span>=<span class="hljs-string">&quot;myPointcut&quot;</span>/&gt;</span><br></code></pre></div></td></tr></table></figure>

<h4 id="基于注解的-AOP-开发"><a href="#基于注解的-AOP-开发" class="headerlink" title="基于注解的 AOP 开发"></a>基于注解的 AOP 开发</h4><ol>
<li><p>创建目标接口和目标类（内部有切点）  </p>
<p><em>目标类</em></p>
 <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Target</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">TargetInterface</span> </span>&#123;<br>   <span class="hljs-comment">//切点</span><br>   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span><span class="hljs-params">()</span> </span>&#123;<br>       System.out.println(<span class="hljs-string">&quot;save running...&quot;</span>);<br>   &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure></li>
<li><p>创建切面类（内部有增强方法）  </p>
<p> <em>切面类</em></p>
 <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAspect</span> </span>&#123;<br>    <span class="hljs-comment">//增强方法</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">before</span><span class="hljs-params">()</span> </span>&#123;<br>       System.out.println(<span class="hljs-string">&quot;before enhance&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure></li>
<li><p>将目标类和切面类的对象创建权交给 spring<br>@Component 注解目标类、切面类注入Spring容器，@Aspect 标注切面类</p>
</li>
<li><p>在切面类中使用注解配置织入关系<br>在增强的方法前直接配置  </p>
</li>
</ol>
<p><em>切面类</em><br>    <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-comment">//配置前置增强</span><br><span class="hljs-meta">@Before(&quot;execution(void com.anno..*.*())&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">before</span><span class="hljs-params">()</span> </span>&#123;<br>    System.out.println(<span class="hljs-string">&quot;before enhance1&quot;</span>);<br>&#125;<br></code></pre></div></td></tr></table></figure></p>
<ol start="5">
<li>在配置文件中开启组件扫描和 AOP 自动代理</li>
</ol>
<p>  <strong>applicationContext.xml（相关配置）</strong><br>  <figure class="highlight xml"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--组件扫描--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.anno&quot;</span>/&gt;</span><br><br><span class="hljs-comment">&lt;!--aop自动代理--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">aop:aspectj-autoproxy</span>/&gt;</span><br></code></pre></div></td></tr></table></figure></p>
<h5 id="详解-1"><a href="#详解-1" class="headerlink" title="详解"></a>详解</h5><p><strong>几种增强方式以及切点表达式的注解实现</strong></p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-meta">@Component</span><br><span class="hljs-meta">@Aspect</span> <span class="hljs-comment">//标注当前类为切面类</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAspect</span> </span>&#123;<br><br>    <span class="hljs-comment">//配置前置增强</span><br>    <span class="hljs-meta">@Before(&quot;execution(void com..*())&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">before</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;before enhance1&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">//配置环绕增强</span><br>    <span class="hljs-meta">@Around(&quot;execution(void com..*())&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">around</span><span class="hljs-params">(ProceedingJoinPoint pjp)</span> <span class="hljs-keyword">throws</span> Throwable </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;before enhance&quot;</span>);<br>        Object proceed = pjp.proceed();<br>        System.out.println(<span class="hljs-string">&quot;after enhance&quot;</span>);<br>        <span class="hljs-keyword">return</span> proceed;<br>    &#125;<br><br>    <span class="hljs-comment">//定义切点表达式</span><br>    <span class="hljs-meta">@Pointcut(&quot;execution(void com..*())&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pointcut</span><span class="hljs-params">()</span> </span>&#123;&#125;<br><br>    <span class="hljs-comment">//配置后置增强</span><br>    <span class="hljs-meta">@AfterReturning(&quot;MyAspect.pointcut()&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">afterreturning</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;afterReturning enhance&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">//配置最终法返回增强</span><br>    <span class="hljs-meta">@After(&quot;pointcut()&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">after</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;after enhance1&quot;</span>);<br>    &#125;<br><br>&#125;<br></code></pre></div></td></tr></table></figure>

<h4 id="注意点"><a href="#注意点" class="headerlink" title="注意点"></a>注意点</h4><p>经过测试发现：<br><img src="/images/pasted-115.png" srcset="/img/loading.gif" lazyload><br>当前置、后置、环绕、最终增强都存在时，增强顺序如图所示。  </p>
<ol>
<li>执行到目标方法</li>
<li>执行环绕增强中的前置增强</li>
<li>准备执行目标方法</li>
<li>执行前置增强</li>
<li>执行目标方法</li>
<li>执行环绕增强的后置增强</li>
<li>执行最终增强</li>
<li>执行后置增强</li>
</ol>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章未经允许，严禁转载！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/05/01/SSM-D10/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">SSM--D10</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/04/29/SM-D8/">
                        <span class="hidden-mobile">SSM--D8</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     CC <i class="iconfont icon-love"></i> XX
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    
      <script  src="/js/img-lazyload.js" ></script>
    
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
